<template>
  <base-page-view class-name="page-container">
    <view class="bind-input">
      <input v-model="bindCode" class="uni-input" type="text" placeholder="请输入您要绑定的邀请码" />
    </view>
    <view class="bind-btn" @click="findInvite">保存</view>
    <confirm-popup ref="confirm">
      <view slot="content">
        <view class="subtitle">推荐人一但绑定无法解绑</view>
        <view class="bind-content">
          <b-image :src="inviteUser.head_img" alt="" />
          <text class="nickname">{{ inviteUser.nickname }}</text>
          <text class="moble">手机号: {{ inviteUser.mobile }}</text>
        </view>
      </view>
    </confirm-popup>
  </base-page-view>
</template>
<script>
import confirmPopup from '@/components/confirmPopup/index'
import './index.scss'
export default {
  components: {
    confirmPopup
  },
  data() {
    return {
      bindCode: '',
      inviteUser: {}
    }
  },
  methods: {
    async findInvite() {
      if (!this.bindCode.trim()) {
        this.$gc.toast('请输入邀请码')
        return
      }
      const res = await this.$api.mineApi.findUserByInviteCode({
        invite_code: this.bindCode
      })
      if (res.success) {
        if (!res.data) {
          this.$gc.toast('无法查询到该用户')
        } else {
          this.inviteUser = res.data
          this.$refs.confirm.show({
            title: '再次确认绑定?',
            content: '',
            yesCb: async() => {
              const res = await this.$api.mineApi.bindInviteCode({
                request_url: 'pagesMine/binCode/index',
                invite_code: this.bindCode
              })
              if (res.success) {
                this.$gc.toast('绑定成功')
                uni.navigateBack()
              }
            }
          })
        }
      }
    }
  }
}
</script>
